package demo2;

import demo1.IntegerComparator;

import java.util.PriorityQueue;

public class Test {
    public static void main(String[] args) {
        int[] arr={1,3,5,7,2,4,6,8};
        int k=4;
        PriorityQueue<Integer> priorityQueue=new PriorityQueue<>(new IntegerComparator());
        for(int i=0;i< k;i++){
            //已经创建好了大根堆
            priorityQueue.offer(arr[i]);
        }
        //进行比较
        for(int i=k;i<arr.length;i++){
            if(priorityQueue.peek()>arr[i]){
                priorityQueue.poll();
                priorityQueue.offer(arr[i]);
            }
        }


    }
}
